<?php

    class itUtilTest extends itTestCase {

        /**
         * @test
         */
        public function inArray() {
            $obj = new StdClass();
            $this->assertTrue(itUtil::inArray($obj, array($obj)));
            $this->assertTrue(itUtil::inArray($obj, array('a' => $obj)));
        }
        
        /**
         * @test
         */
        public function getClassAndNS() {
            list($namespace, $class) = itUtil::getClassAndNS('Intelligencer\Factity');
            $this->assertEquals('Intelligencer', $namespace);
            $this->assertEquals('Factity', $class);
            
            list($namespace, $class) = itUtil::getClassAndNS(array('Intelligencer', 'Factity'));
            $this->assertEquals('Intelligencer', $namespace);
            $this->assertEquals('Factity', $class);
            
            list($namespace, $class) = itUtil::getClassAndNS('Factity');
            $this->assertEquals(null, $namespace);
            $this->assertEquals('Factity', $class);
            
            list($namespace, $class) = itUtil::getClassAndNS(array('', 'Factity'));
            $this->assertEquals(null, $namespace);
            $this->assertEquals('Factity', $class);
            
            list($namespace, $class) = itUtil::getClassAndNS('A\B\C\Factity');
            $this->assertEquals('A\B\C', $namespace);
            $this->assertEquals('Factity', $class);
            
            list($namespace, $class) = itUtil::getClassAndNS(array('A\B\C', 'Factity'));
            $this->assertEquals('A\B\C', $namespace);
            $this->assertEquals('Factity', $class);
        }
        
        /**
         * @test
         */
        public function getLowerClassAndNS() {
            list($namespace, $class) = itUtil::getLowerClassAndNS('Intelligencer\Factity');
            $this->assertEquals('intelligencer', $namespace);
            $this->assertEquals('factity', $class);
            
            list($namespace, $class) = itUtil::getLowerClassAndNS(array('Intelligencer', 'Factity'));
            $this->assertEquals('intelligencer', $namespace);
            $this->assertEquals('factity', $class);
        }
        
        /**
         * @test
         */
        public function trimSlashes() {
            $this->assertEquals('aa', itUtil::trimSlashes('/aa/'));
            $this->assertEquals('aa', itUtil::trimSlashes('\aa\\'));
            $this->assertEquals('aa', itUtil::trimSlashes('\aa/'));
        }
        
        /**
         * @test
         */
        public function getRealPath() {
            $this->assertNotNull(itUtil::getRealPath(TEST_DATA.'/Shared/JustFile/'));
            $this->assertNotNull(itUtil::getRealPath(TEST_DATA.'/Shared/JustFile'));
            $this->assertNotNull(itUtil::getRealPath(TEST_DATA.'/Shared/JustFile/hey.php'));
            $this->assertNull(itUtil::getRealPath(TEST_DATA.'/Shared/JustFile/heyDoesNotExist.php'));

        }
        
        public function testPharRealPath() {
            if (!extension_loaded('Phar')) {
                return;
            }
            $this->assertNotNull(itUtil::getRealPath(TEST_DATA.'/Shared/Phar/test.phar'));
            $this->assertNotNull(itUtil::getRealPath('phar://'.TEST_DATA.'/Shared/Phar/test.phar'));
            $this->assertNotNull(itUtil::getRealPath('phar://'.TEST_DATA.'/Shared/Phar/test.phar/config'));
            $this->assertNotNull(itUtil::getRealPath('phar://'.TEST_DATA.'/Shared/Phar/test.phar/config/FactityConfig.php'));
            $this->assertNull(itUtil::getRealPath('phar://'.TEST_DATA.'/Shared/Phar/test.phar/DoesNotExist.php'));
        }
        
        /**
         * @test
         */
        public function minusArrays() {
            $this->assertEquals(array('a', 'b'), itUtil::minusArrays(array('a', 'b', 'c', 'd'), array('c', 'd')));
        }
        
        /**
         * @test
         */
        public function inArrayInsensitive() {
            $this->assertTrue(itUtil::inArrayInsensitive('aa', array('AA', 'bb')));
            $this->assertTrue(itUtil::inArrayInsensitive('AA', array('bb', 'aa')));
        }
        
        /**
         * @test
         */
        public function arrayCartesian() {
            $a1 = array('a', 'b');
            $a2 = array('c', 'd', 'e');
            $result = array(
                array('a', 'c'),
                array('a', 'd'),
                array('a', 'e'),
                array('b', 'c'),
                array('b', 'd'),
                array('b', 'e'),
            );
            
            $this->assertEquals($result, itUtil::arrayCartesian(array($a1, $a2)));
        }
        
        /**
         * @test
         */
        public function arrayCartesianKeys() {
            $in = array('X' => array('a', 'b'), 'Y' => array('c', 'd', 'e'));
            $result = array(
                array('X' => 'a', 'Y' => 'c'),
                array('X' => 'a', 'Y' => 'd'),
                array('X' => 'a', 'Y' => 'e'),
                array('X' => 'b', 'Y' => 'c'),
                array('X' => 'b', 'Y' => 'd'),
                array('X' => 'b', 'Y' => 'e'),
            );
            
            $this->assertEquals($result, itUtil::arrayCartesianKeys($in));
        }
        
    }

?>